package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.ProductDao;
import pojo.Cart;
import pojo.Customer;
import pojo.PageBean;
import pojo.Product;
import service.CartService;
import service.ProductService;

/**
 * Servlet implementation class CartServlet
 */
@WebServlet("/CartServlet")
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		if(method.equals("add")) {
			add(request,response);
		}else if(method.equals("showAll")) {
			showAll(request,response);
		}else if(method.equals("del")) {
			del(request,response);
		}else if(method.equals("show")) {
			show(request,response);
		}
	}
	private void show(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		//HttpSession session=((HttpServletRequest)request).getSession();
		//Customer cu =(Customer)(session.getAttribute("c"));
		//CartService cservice=new CartService();
		//Cart rt=new Cart();
		//调用showCustomer方法
		//List<Cart> list = cservice.showCartsByUID(cu.getC_name());
		//request.setAttribute("list", list);
	}

	private void del(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String cid=request.getParameter("pid");
		CartService cservice = new CartService();
		int flag = cservice.delCart(cid);
		if(flag>0){
		try {
			response.sendRedirect("cart.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}else {
			try {
				response.sendRedirect("fail.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void showAll(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int currentPage =Integer.parseInt(request.getParameter("currentPage"));
		int pageSize = 5;
		//调用service方法
		ProductService pservice = new ProductService();
		PageBean<Product> pb= pservice.show( currentPage, pageSize);
		request.setAttribute("pb", pb);
		try {
			request.getRequestDispatcher("showproduct.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String pid=request.getParameter("pid");
		HttpSession session=((HttpServletRequest)request).getSession();
		Customer cu=(Customer)session.getAttribute("c");
		ProductDao pdao=new ProductDao();
		Product p =pdao.getOneProduct(pid);
		Cart c=new Cart();
		c.setC_name(cu.getC_name());
		c.setP_id(pid);
		c.setP_name(p.getP_name());
		c.setP_price(p.getP_price());
		c.setAmount(c.getP_price()*c.getQuantity());
		c.setP_image(p.getP_image());
		c.setQuantity(p.getP_quantity());
		CartService cs=new CartService();
		int flag=cs.buyCart(c);
		if(flag>0) {
			try {
				request.getRequestDispatcher("showCart.jsp").forward(request, response);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			try {
				response.sendRedirect("error.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}


}
